package com.distributedApp.app.web;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import DB.RmiClient;
import DB.RmiInterface;

@CrossOrigin
@RestController
public class SearchController {
	
	RmiInterface r;
	
	
	{
		try {
			r = (RmiInterface)RmiClient.getClient().r;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("search/getSearchResult")
	public List getSearchResult() throws MalformedURLException, RemoteException, NotBoundException {
		RmiInterface r=(RmiInterface)RmiClient.getClient().r;
//		r.connect("sqlserver","win.markchen7788.xyz","8893","distributed_db","cq","Zhangshuaixiaozu123");
//		r.connect("mysql","markchen7788.xyz","3306","distributed_db","cq","zhangshuaixiaozu");
		return ((RmiInterface) DB.RmiClient.getClient().r).getSearchResult("select * from card_copy1");
	}
	
	@RequestMapping("search/getStuList")
	public List getStuList(String nameOrId) throws RemoteException{
		System.out.println(nameOrId);
		String sql = "";
		List<HashMap<String, Object>> ret = new ArrayList<HashMap<String,Object>>();
		if(nameOrId==null){
			System.out.println("null name search");
			sql = "SELECT * FROM student WHERE type=1";
			ret = r.getSearchResult(sql);
		}else{
			if(nameOrId.isEmpty()){
				System.out.println("null name search");
				sql = "SELECT * FROM student WHERE type=1";
				ret = r.getSearchResult(sql);
			}else{
				sql = "SELECT * FROM student WHERE type=1 AND id = "+nameOrId;
				ret = r.getSearchResult(sql);
				sql = "SELECT * FROM student WHERE type=1 AND name = '"+nameOrId+"'";
				ret.addAll(r.getSearchResult(sql));
			}
			
		}
		return ret;
	}
	
	@RequestMapping("search/getTchrList")
	public List getTchrList(String nameOrId) throws RemoteException{
		System.out.println(nameOrId);
		String sql = "";
		List<HashMap<String, Object>> ret = new ArrayList<HashMap<String,Object>>();
		if(nameOrId==null){
			System.out.println("null name search");
			sql = "SELECT * FROM student WHERE type=2";
			ret = r.getSearchResult(sql);
		}else{
			if(nameOrId.isEmpty()){
				System.out.println("null name search");
				sql = "SELECT * FROM student WHERE type=2";
				ret = r.getSearchResult(sql);
			}else{
				sql = "SELECT * FROM student WHERE type=2 AND id = "+nameOrId;
				ret = r.getSearchResult(sql);
				sql = "SELECT * FROM student WHERE type=2 AND name = '"+nameOrId+"'";
				ret.addAll(r.getSearchResult(sql));
			}
			
		}
		return ret;
	}
	
	@RequestMapping("search/getRcdList")
	public List getRcdList(Integer id) throws RemoteException{
		System.out.println(id);
		String sql = "";
		List<HashMap<String, Object>> ret = new ArrayList<HashMap<String,Object>>();
		if(id==null){
			System.out.println("null id search");
			sql = "SELECT * FROM student RIGHT JOIN card_copy1 ON student.id = card_copy1.studentId";
			ret = r.getSearchResult(sql);
		}else{
			sql = "SELECT * FROM student RIGHT JOIN card_copy1 ON student.id = card_copy1.studentId WHERE student.id = "+id;
			ret = r.getSearchResult(sql);
		}
		
		return ret;
	}
	
	@RequestMapping("search/deleteStu")
	public boolean deleteStu(int id) throws RemoteException{
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "delete");
		sql.put("tbName", "student");
		sql.put("where", "id = " + id);
		
		return r.manipulate(sql);
	}
	
	@RequestMapping("search/deleteTchr")
	public boolean deleteTchr(int id) throws RemoteException{
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "delete");
		sql.put("tbName", "student");
		sql.put("where", "id = " + id);
		return r.manipulate(sql);
	}
	
	@RequestMapping("search/deleteStuL")
	public boolean deleteStuL(String id) throws RemoteException{
		System.out.println(id);
		Pattern pattern = Pattern.compile("^\\D+|\\D+$");
		Matcher matcher = pattern.matcher(id);
		id = matcher.replaceAll("");
		System.out.println(id);
		String[] idsStr = id.split(",");
		ArrayList<Integer> ids = new ArrayList<Integer>();
		for(String i : idsStr){
			ids.add(Integer.parseInt(i));
		}
		
		
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "false");
		r.manipulate(sql);
		try{
			for (int i : ids){
				sql = new HashMap<String, Object>();
				sql.put("manipulation", "delete");
				sql.put("tbName", "student");
				sql.put("field", "");
				sql.put("where", "id = " + i);
				r.manipulate(sql);
			}
		}catch (Exception e) {
			//捕捉到异常则回滚
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "rollback");
			r.manipulate(sql);
			//打开自动提交
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "true");
			r.manipulate(sql);
			return false;
		}
		//未捕捉到异常则提交
		try{
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "commit");
			r.manipulate(sql);
			return true;
		}catch (Exception e) {
			//捕捉到异常则回滚
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "rollback");
			r.manipulate(sql);
			
			//打开自动提交
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "true");
			r.manipulate(sql);
			return false;
		}
	}
	
	@RequestMapping("search/deleteTchrL")
	public boolean deleteTchrL(String id) throws RemoteException{
		System.out.println(id);
		Pattern pattern = Pattern.compile("^\\D+|\\D+$");
		Matcher matcher = pattern.matcher(id);
		id = matcher.replaceAll("");
		System.out.println(id);
		String[] idsStr = id.split(",");
		ArrayList<Integer> ids = new ArrayList<Integer>();
		for(String i : idsStr){
			ids.add(Integer.parseInt(i));
		}
		
		
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "false");
		r.manipulate(sql);
		try{
			for (int i : ids){
				sql = new HashMap<String, Object>();
				sql.put("manipulation", "delete");
				sql.put("tbName", "student");
				sql.put("field", "");
				sql.put("where", "id = " + i);
				r.manipulate(sql);
			}
		}catch (Exception e) {
			//捕捉到异常则回滚
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "rollback");
			r.manipulate(sql);
			//打开自动提交
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "true");
			r.manipulate(sql);
			return false;
		}
		//未捕捉到异常则提交
		try{
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "commit");
			r.manipulate(sql);
			return true;
		}catch (Exception e) {
			//捕捉到异常则回滚
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "rollback");
			r.manipulate(sql);
			
			//打开自动提交
			sql = new HashMap<String, Object>();
			sql.put("manipulation", "true");
			r.manipulate(sql);
			return false;
		}
	}
	
	@RequestMapping("search/insertTchr")
	public boolean insertTchr(Integer yuanid,String name) throws RemoteException{
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "insert");
		sql.put("tbName", "student");
		HashMap<String, Object> ins = new HashMap<String, Object>();
		ins.put("name", name);
		ins.put("type", "2");
		sql.put("field", ins);
		
		return r.manipulate(sql);
	}
	
	@RequestMapping("search/insertStu")
	public boolean insertStu(Integer yuanid,String name) throws RemoteException{
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "insert");
		sql.put("tbName", "student");
		HashMap<String, Object> ins = new HashMap<String, Object>();
		ins.put("name", name);
		ins.put("type", "1");
		sql.put("field", ins);
		
		return r.manipulate(sql);
	}
	


	@RequestMapping("search/updateTchr")
	public boolean updateTchr(Integer yuanid,String name) throws RemoteException{
		System.out.println(yuanid);
		System.out.println(name);
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "update");
		sql.put("tbName", "student");
		sql.put("where", "id = "+yuanid);
		HashMap<String, Object> ins = new HashMap<String, Object>();
		ins.put("name", name);
		sql.put("field", ins);
		
		return r.manipulate(sql);
	}
	
	@RequestMapping("search/updateStu")
	public boolean updateStu(Integer yuanid,String name) throws RemoteException{
		System.out.println(yuanid);
		System.out.println(name);
		HashMap<String, Object> sql = new HashMap<String, Object>();
		sql.put("manipulation", "update");
		sql.put("tbName", "student");
		sql.put("where", "id = "+yuanid);
		HashMap<String, Object> ins = new HashMap<String, Object>();
		ins.put("name", name);
		sql.put("field", ins);
		
		return r.manipulate(sql);
	}
	
	@RequestMapping("search/changeDB")
	public boolean changeDB(String changeToWhat) throws RemoteException{
		try{
			switch (changeToWhat){
			case "MySQL":
				r.close();
				r.connect("mysql","markchen7788.xyz","3306","distributed_db","cq","zhangshuaixiaozu");
				break;
			case "SqlServer":
				r.close();
				r.connect("sqlserver","win.markchen7788.xyz","8893","distributed_db","cq","Zhangshuaixiaozu123");
				break;
			default:
				return false;
			}
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("changeDB Exception");
			return false;
		}
		
		return true;
	}
}
